<?php
/**
 * @version        $Id$
 * @author         master@xuewl.com
 * @copyright      Copyright (c) 2007 - 2014, Chongqing xuewl Information Technology Co., Ltd.
 * @link           http://www.xuewl.com
**/
namespace Admin\Controller;
define('MODULE_CACHE', DATA_PATH.'caches_model/');
class NewtaskController extends InitController {
    public function _initialize() {
    	parent::_initialize();
        $this->categorys = getcache('category', 'commons');
        $this->modelid = 2;
        $this->models = getcache('model', 'commons');
        $this->userid = (int) cookie('_userid');
        $this->db = D('Newtask');
        /* 将页码变量赋值到模板 */
    }

    /* 任务管理 */
    public function manage() {
        $page = I('page', '0', 'int');
        $count = $this->db->count();
        $infos = $this->db->order("id DESC")->page($page, 10)->select();
        $pages = page($count, 10);
        include $this->admin_tpl('newtask_manage');
    }

    /* 添加任务 */
    public function add() {
    	if (submitcheck('dosubmit')) {
    		$info = $_POST['info'];
            $result = $this->db->update($info);
            if (!$result) {
                $this->error($this->db->getError());
            }
            $this->success('任务添加成功');
    	} else {
    		include $this->admin_tpl('newtask_add');
    	}
    }

    public function edit() {
        $id = I('id', 0, 'intval');
        $data = $this->db->getById($id);
    	if (submitcheck('dosubmit')) {
    		$info = $_POST['info'];
            $info['id'] = $id;
            $result = $this->db->update($info);
            if(!$result) {
                $this->error($this->db->getError());
            }
            $this->success('任务编辑成功');
    	} else {
            $data['configs'] = string2array($data['configs']);
    		include $this->admin_tpl('newtask_edit');
    	}
    }

    /* 删除任务 */
    public function delete() {
        if (submitcheck('dosubmit', 'GP')) {
            $ids = (array) I('id');
            if(empty($ids)) $this->error('请选择要删除的项目');
            $sqlmap = array();
            $sqlmap['id'] = array("IN", $ids);
            $this->db->where($sqlmap)->delete();
            $this->success('操作成功');
        } else {
            $this->error('请勿非法操作');
        }
    }
}